/*
** delete2.c for minishell2 in /home/lingla_j/work/tutorat/env
** 
** Made by julien linglain
** Login   <lingla_j@epitech.net>
** 
** Started on  Tue Feb 28 17:46:54 2012 julien linglain
** Last update Sat Mar  3 15:11:00 2012 julien linglain
*/

#include <stdlib.h>
#include <string.h>
#include "mysh.h"

int             check_exception(t_list *env, char *key)
{
  t_node        *ptr;

  ptr = env->start;
  if (env->size == 1)
    {
      env->start = NULL;
      env->end = NULL;
      free(ptr->key);
      free(ptr->value);
      free(ptr);
      env->size--;
      return (1);
    }
  else if (env->size == 0)
    {
      my_puterror("Env is empty.\n");
      return (1);
    }
  return (0);
}

int		delete_list(t_list *env)
{
  t_node	*tmp;

  tmp = env->start;
  env->start = env->start->next;
  free(tmp->key);
  free(tmp->value);
  env->size--;
  return (0);
}

int		delete_list_last(t_list *env)
{
  t_node	*tmp;

  tmp = env->start;
  env->start = NULL;
  env->end = NULL;
  free(tmp->key);
  free(tmp->value);
  env->size--;
  return (0);
}

int		delete_list_all(t_list *env)
{
  while (env->size > 0)
    {
      if (env->size > 1)
	delete_list(env);
      else
	delete_list_last(env);
    }
  return (0);
}
